#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.enumhandle;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.EnumTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 枚举类型转换器
 *
 * @author: caoqiang
 * @create: 2019-05-21 17:25
 **/

public class AutoEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {

  private BaseTypeHandler typeHandler;

  public AutoEnumTypeHandler(Class<E> type) {
    if (type == null) {
      throw new IllegalArgumentException("Type argument cannot be null");
    }
    if (BaseEnum.class.isAssignableFrom(type)) {
      // 如果实现了 BaseCodeEnum 则使用我们自定义的转换器
      typeHandler = new CodeEnumTypeHandler(type);
    } else {
      // 默认转换器 也可换成 EnumOrdinalTypeHandler
      typeHandler = new EnumTypeHandler<>(type);
    }
  }

  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
    typeHandler.setNonNullParameter(ps, i, parameter, jdbcType);
  }

  @Override
  public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
    return (E) typeHandler.getNullableResult(rs, columnName);
  }

  @Override
  public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    return (E) typeHandler.getNullableResult(rs, columnIndex);
  }

  @Override
  public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    return (E) typeHandler.getNullableResult(cs, columnIndex);
  }
}
